package com.example.minzhengtong

import android.app.Activity
import android.app.ActivityManager
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.os.StrictMode
import android.util.Log
import androidx.multidex.MultiDex
import com.example.minzhengtong.base.BaseApp
import com.example.minzhengtong.utils.LogUtil
import kotlin.properties.Delegates


/**
 * Created by LFC
on 2020/5/13.
 */
open class MyApp : BaseApp() {

    companion object {

        var isForeground: Boolean = true
        val TAG = "orange"

        var context: Context by Delegates.notNull()
            private set

        lateinit var instance: MyApp

        var isAuth = false

    }

    override fun onCreate() {
        super.onCreate()
        LogUtil.d("-----application----onCreate")
        instance = this
        context = applicationContext
        registerActivityLifecycleCallbacks(mActivityLifecycleCallbacks)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            val builder = StrictMode.VmPolicy.Builder()
            StrictMode.setVmPolicy(builder.build())
        }
    }

    private fun getAppName(pID: Int): String {
        var processName: String = ""
        val am: ActivityManager = this.getSystemService(ACTIVITY_SERVICE) as ActivityManager
        val l: List<*> = am.getRunningAppProcesses()
        val i = l.iterator()
        val pm: PackageManager = this.packageManager
        while (i.hasNext()) {
            val info: ActivityManager.RunningAppProcessInfo =
                i.next() as ActivityManager.RunningAppProcessInfo
            try {
                if (info.pid === pID) {
                    processName = info.processName
                    return processName
                }
            } catch (e: Exception) {
                // Log.d("Process", "Error>> :"+ e.toString());
            }
        }
        return processName
    }


    override fun attachBaseContext(base: Context) {
//        super.attachBaseContext(MultiLanguageUtils.attachBaseContext(base))
        LogUtil.d("触发了attachBaseContext")
        super.attachBaseContext(base)
        MultiDex.install(this)
    }

    private var activityCount: Int = 0

    private val mActivityLifecycleCallbacks = object : ActivityLifecycleCallbacks {
        override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
            Log.d(TAG, "onCreated: " + activity.componentName.className)
        }

        override fun onActivityStarted(activity: Activity) {
            Log.d(TAG, "onStart: " + activity.componentName.className)
            if (activityCount == 0) {
                isForeground = true
            }
            activityCount++
        }

        override fun onActivityResumed(activity: Activity) {

        }

        override fun onActivityPaused(activity: Activity) {

        }

        override fun onActivityStopped(activity: Activity) {
            activityCount--
            if (activityCount == 0) {
//                timer.cancel()
                isForeground = false
            }
        }

        override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {

        }

        override fun onActivityDestroyed(activity: Activity) {
            Log.d(TAG, "onDestroy: " + activity.componentName.className)
        }
    }


}